package com.valtech.poc.hiddenapi;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;


public class MainActivity extends Activity {

    private final static String TAG = "Valtech";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button b = (Button) findViewById(R.id.endCallButton);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                accessHiddenEndCall();
            }
        });

        Button b2 = (Button) findViewById(R.id.radioOffButton);
        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                accessHiddensRadioPower();
            }
        });

        Button b3 = (Button) findViewById(R.id.listAPIButton);
        b3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                logTelephonyAPI();
            }
        });
    }

    private void logTelephonyAPI(){
        TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        // retrieve all method names
        for(Method m : tm.getClass().getMethods()){
            if(m != null){
                Log.i(TAG, "Method found: " + m.getName());
                if(m.getName().equals("setRadioPower")){
                    for(Type t : m.getParameterTypes()){
                        Log.e(TAG, "Parameter type:" + t.toString());
                    }
                }
            }
        }
        // retrieve all attributes
        for(Field f : tm.getClass().getFields()){
            if(f != null){
                Log.i(TAG, "Field found: " + f.getName());
            }
        }
    }

    private void accessHiddenEndCall(){
        // Retrieve the TelephonyManager instance
        TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

        String  methodName = "endCall";
        try{
            // method prototype : public boolean endCall()
            Method m = tm.getClass().getMethod(methodName);
            m.invoke(tm);
        }
        catch (NoSuchMethodException e){
            Log.e(TAG, "Method "+methodName+" does not exists");
        } catch (InvocationTargetException e) {
            Log.e(TAG, "An error occurs INSIDE "+methodName+" method : "+e.getMessage());
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            Log.e(TAG, "Access to method "+methodName+" restricted");
        }
    }

    private void accessHiddensRadioPower(){
        // Retrieve the TelephonyManager instance
        TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        String  methodName = "setRadioPower";
        try{
            // method prototype: public boolean setRadioPower(boolean turnOn)
            Method m = tm.getClass().getMethod(methodName, boolean.class);
            Boolean isTurnedOn = (Boolean) m.invoke(tm, true);
        }
        catch (NoSuchMethodException e){
            Log.e(TAG, "Method "+methodName+" does not exists");
        } catch (InvocationTargetException e) {
            Log.e(TAG, "An error occurs INSIDE "+methodName+" method : " + e.getCause().getMessage());
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            Log.e(TAG, "Access to method "+methodName+" restricted");
        }
    }
}
